Page({
  data: {
    fontSize: 16,
    bgColor: '#f6f6f6',
    colors: ['#f6f6f6', '#ffffff', '#111111', '#faf0e6', '#e6fffb'],
    colorIndex: 0,
    username: ''
  },
  onShow(){
    const s = wx.getStorageSync('uiSettings') || {}
    this.setData({
      fontSize: s.fontSize || 16,
      bgColor: s.bgColor || '#f6f6f6',
      colorIndex: Math.max(0, this.data.colors.indexOf(s.bgColor || '#f6f6f6'))
    })
    this.setData({ username: wx.getStorageSync('username') || '' })
  },
  onName(e){ this.setData({ username: e.detail.value }) },
  onFont(e){ this.setData({ fontSize: e.detail.value }) },
  onColor(e){
    const idx = Number(e.detail.value)
    this.setData({ colorIndex: idx, bgColor: this.data.colors[idx] })
  },
  save(){
    wx.setStorageSync('uiSettings', { fontSize: this.data.fontSize, bgColor: this.data.bgColor })
    wx.setStorageSync('username', this.data.username)
    wx.showToast({ title: '已保存', icon: 'success' })
  }
})

